// pages/orderDetail/orderDetail.js
var app = getApp();
var host = app.globalData.host;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        flag: 0,
        addresses: '',
        goodsId: '',
        goodsDetail: null,
        num: 1,
        addressId: '',
        totalPrice: 0,
        orderDetail: null,
        orderId: null,
        sendtime: null,
        // 快捷进入

        // addresses: '',
        // goodsId: 9,
        // goodsDetail: null,
        // num: 1,
        // addressId: '',
        // totalPrice: 83.76,
        // orderDetail: null,
        // orderId: 8793,
        // sendtime: new Date(),

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(e) {
        console.log(e);
        var that = this;
        var orderId = 8793;
        this.setData({ orderId: orderId });
        this.loadOrder(orderId);

    },
    loadOrder: function(orderId) {
        var page = this;
        if (orderId != null && orderId != "") {
            wx.request({
                url: host + '/api/order/getOrderById',
                method: 'GET',
                data: {
                    "orderId": orderId
                },
                header: {
                    'Content-Type': 'application/json'
                },
                success: function(res) {
                    console.log(res);
                    var code = res.data.code;
                    var orderDetail = res.data.data;
                    if (code = '0000') {
                        var addressId = orderDetail.addressId;
                        console.log(addressId);
                        // 获取时间
                        // var currentTime = new Date();
                        var sendtime = orderDetail.createTime;

                        console.log(sendtime);
                        page.setData({ orderDetail: orderDetail });
                        page.loadAddress(orderDetail.addressId);
                        page.loadGoods(orderDetail.goodsId);
                        page.setData({ goodsId: orderDetail.goodsId });
                        page.setData({ num: orderDetail.num });
                        page.setData({ addressId: orderDetail.addressId });
                        page.setData({ sendtime: orderDetail.createTime });
                    }
                }
            })
        }
    },

    loadGoods: function(goodsId) {
        var page = this;
        if (goodsId != null && goodsId != '') {
            wx.request({
                url: host + '/api/goods/getGoodsDetail',
                method: 'GET',
                data: {
                    "goodsId": goodsId
                },
                header: {
                    'Content-Type': 'application/json'
                },
                success: function(res) {
                    console.log(res);
                    var code = res.data.code;
                    // var sendtime = res.header.Date;
                    var goodsDetail = res.data.data;
                    console.log(goodsDetail);
                    if (code = '0000') {
                        // page.setData({ sendtime: sendtime });
                        page.setData({ goodsDetail: goodsDetail });
                        // console.log(page.data);
                        var num = page.data.num;
                        //计算总价格
                        var totalPrice = goodsDetail.goodsPrice * num;

                        page.setData({ totalPrice: totalPrice.toFixed(2) });
                        // console.log(totalPrice);
                        // console.log(num);
                    }
                }
            })
        }
    },

    loadAddress: function(id) {
        var page = this;
        if (id != null && id != "") {
            wx.request({
                // url: host + '/api/address/selectAddressByUserId',根据用户id来循环，之前address.js写的
                //这里是需要通过地址id
                url: host + '/api/address/getAddressById',
                method: 'GET',
                data: {
                    // "userId": userId
                    "id": id
                },
                header: {
                    'Content-Type': 'application/json'
                },
                success: function(res) {
                    console.log(res)
                    var code = res.data.code;
                    var addresses = res.data.data;
                    if (code = '0000') {
                        page.setData({ addresses: addresses });
                    }
                    console.log(addresses)
                }
            })
        } else {
            // wx.redirectTo({
            //     url: '../login/login'
            // })
        }
    },
    buy: function() {
        wx.switchTab({
            url: '../index/index',
            success: (result) => {

            },
            fail: () => {},
            complete: () => {}
        });
    }




})